﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using HarrisDesigns.Controls.dasBlogControls.Google.Reader;

namespace HarrisDesigns.Controls.dasBlogControls
{
    [DefaultProperty("GoogleAccountIdentity")]
    [ToolboxData(@"<{0}:GoogleReaderSharedItemsControl googleAccountIdentity=""usernumber"" itemCount=""10"" runat=""server""></{0}:GoogleReaderSharedItemsControl>")]
    public class GoogleReaderSharedItemsControl : Panel
    {
        private const string PollingMinutesViewStateIdentifier = "PollingMinutes";
        private const string ItemCountViewStateIdentifier = "ItemCount";
        private const string GoogleIdentityViewStateIdentifier = "User";
        private const string SharedItemCssClass = "GoogleReaderSharedItem";
        private const string SharedItemTimestampCssClass = "GoogleReaderSharedItemTimestamp";

        public GoogleReaderSharedItemsControl()
        {
            Load += ControlLoad;
        }

        [Category("Behavior")]
        [DefaultValue("")]
        public string GoogleAccountIdentity
        {
            get
            {
                var s = (String)ViewState[GoogleIdentityViewStateIdentifier];
                return (s ?? String.Empty);
            }

            set { ViewState[GoogleIdentityViewStateIdentifier] = value; }
        }

        [Category("Appearance")]
        [DefaultValue("10")]
        public int ItemCount
        {
            get
            {
                object b = ViewState[ItemCountViewStateIdentifier];
                return ((b is int) ? (int)b : 10);
            }

            set { ViewState[ItemCountViewStateIdentifier] = value; }
        }

        [Category("Behavior")]
        [DefaultValue("180")]
        public int PollingMinutes
        {
            get
            {
                object b = ViewState[PollingMinutesViewStateIdentifier];
                return ((b is int) ? (int)b : 180);
            }

            set { ViewState[PollingMinutesViewStateIdentifier] = value; }
        }

        private void ControlLoad(object sender, EventArgs e)
        {
            Repository repository = new Repository { UserIdentity = GoogleAccountIdentity, MaxItems = ItemCount, PollingInterval = PollingMinutes };
            Feed userFeed = repository.GetCurrentData();
            foreach (FeedItem feedItem in userFeed.FeedItems)
            {
                var itemPanel = new Panel();
                Controls.Add(itemPanel);
                itemPanel.CssClass = SharedItemCssClass;

                var itemText = new HyperLink();
                itemPanel.Controls.Add(itemText);
                itemText.Text = feedItem.Title;
                itemText.NavigateUrl = feedItem.Alternate.Href;

                var itemTimestamp = new Label();
                itemPanel.Controls.Add(itemTimestamp);
                itemTimestamp.CssClass = SharedItemTimestampCssClass;
                itemTimestamp.Text = feedItem.Published.ToShortDateString();
            }
        }
    }
}